using System;
using System.Collections;
using System.Xml;

namespace System.Web.Core
{
    public class TableMapping
    {
        private Type entityType = null;
        private string tableName = null;
        private string createCommandIdent = null;
        private string retrieveCommandIdent = null;
        private string updateCommandIdent = null;
        private string deleteCommandIdent = null;
        private Hashtable fieldMappings = new Hashtable();

        public Type EntityType
        {
            get
            {
                return entityType;
            }
            set
            {
                entityType = value;
            }
        }
        public string TableName
        {
            get
            {
                return tableName;
            }
            set
            {
                tableName = value;
            }
        }
        public string CreateCommandIdent
        {
            get
            {
                return createCommandIdent;
            }
            set
            {
                createCommandIdent = value;
            }
        }
        public string RetrieveCommandIdent
        {
            get
            {
                return retrieveCommandIdent;
            }
            set
            {
                retrieveCommandIdent = value;
            }
        }
        public string UpdateCommandIdent
        {
            get
            {
                return updateCommandIdent;
            }
            set
            {
                updateCommandIdent = value;
            }
        }
        public string DeleteCommandIdent
        {
            get
            {
                return deleteCommandIdent;
            }
            set
            {
                deleteCommandIdent = value;
            }
        }
        public Hashtable FieldMappings
        {
            get
            {
                return fieldMappings;
            }
            set
            {
                fieldMappings = value;
            }
        }

        public TableMapping(XmlNode xmlNode)
        {
            if (xmlNode.Attributes["entityType"] != null)
            {
                entityType = Type.GetType(xmlNode.Attributes["entityType"].Value);
            }
            if (xmlNode.Attributes["tableName"] != null)
            {
                tableName = xmlNode.Attributes["tableName"].Value;
            }
            if (xmlNode.Attributes["createCommand"] != null)
            {
                createCommandIdent = xmlNode.Attributes["createCommand"].Value;
            }
            if (xmlNode.Attributes["retrieveCommand"] != null)
            {
                retrieveCommandIdent = xmlNode.Attributes["retrieveCommand"].Value;
            }
            if (xmlNode.Attributes["updateCommand"] != null)
            {
                updateCommandIdent = xmlNode.Attributes["updateCommand"].Value;
            }
            if (xmlNode.Attributes["deleteCommand"] != null)
            {
                deleteCommandIdent = xmlNode.Attributes["deleteCommand"].Value;
            }

            string propertyName;

            foreach (XmlNode fieldMappingNode in xmlNode.SelectNodes("fieldMapping"))
            {
                propertyName = fieldMappingNode.Attributes["propertyName"].Value;
                fieldMappings[propertyName] = new FieldMapping(propertyName, fieldMappingNode.Attributes["fieldName"].Value, fieldMappingNode.Attributes["fieldDBType"].Value, fieldMappingNode.Attributes["fieldDBTypeEnum"].Value, int.Parse(fieldMappingNode.Attributes["fieldSize"].Value));
            }
        }
    }
}
